package com.base.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.base.api.sys.ISysEnvironmentService;
import com.base.api.sys.ISysMenuService;
import com.base.api.sys.SysCacheService;
import com.base.utils.ExcuteLinux;
import com.base.vo.sys.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/sys", produces = "application/json;charset=UTF-8")
public class SysController {

    private final ISysMenuService iSysMenuService;

    private final ISysEnvironmentService iSysEnvironmentService;

    @Autowired
    private SysCacheService sysCacheService;

    @Autowired
    public SysController(ISysMenuService iSysMenuService, ISysEnvironmentService iSysEnvironmentService) {
        this.iSysMenuService = iSysMenuService;
        this.iSysEnvironmentService = iSysEnvironmentService;
    }

    @GetMapping(value = "/allMenu")
    public String allMenu() {
        return JSONObject.toJSONString(iSysMenuService.findTree());
    }

    @GetMapping(value = "/allSysMenu")
    public String allMenuMap() {
        return JSONObject.toJSONString(iSysMenuService.findSysMenu());
    }

    @PostMapping("/saveSysMenu")
    public String saveSysMenu(MenuVo sysMenu) {
        return iSysMenuService.save(sysMenu);
    }

    @PostMapping("/deleteSysMenu")
    public String deleteSysMenu(Integer id) {
        return iSysMenuService.delete(id);
    }

    @GetMapping("/clearCache")
    public Map<String, Object> clearCache(){
        return iSysMenuService.clearCache();
    };

    @GetMapping("/excuteLinux")
    public String executeLinux(String command){
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("data",ExcuteLinux.exeCmd(command));
        return JSONObject.toJSONString(map);
    };

    @GetMapping(value = "/getAllCacheKey")
    public String getAllCacheKey() {
        return sysCacheService.getAllKey();
    }

    @GetMapping(value = "/getCacheBykey")
    public String getCacheBykey(String keyName) {
        return sysCacheService.getCacheByKeyName(keyName);
    }

    @GetMapping(value = "/delCacheBykeyName")
    public String delCacheBykeyName(String keyName) {
        return sysCacheService.delCacheByKeyName(keyName);
    }
}
